package com.example.hr.text_android.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.example.hr.text_android.R;
import com.example.hr.text_android.uitis.AppUtils;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.tools.DebugUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by hr on 2017/8/3.
 */
public class GridPictureAdapter extends
        RecyclerView.Adapter<GridPictureAdapter.ViewHolder> {

    public static final int TYPE_CAMERA = 1; //不够九张
    public static final int TYPE_PICTURE = 2;
    private List<LocalMedia> list = new ArrayList<>();
    private LayoutInflater mInflater;
    private Context context;
    private int selectMax = 9; //最大可选择几张图片

    public GridPictureAdapter(Context context, OnAddPicClickListener onAddPicClickListener) {
        this.context = context;
        mInflater = LayoutInflater.from(context);
        this.onAddPicClickListener = onAddPicClickListener;
    }

    /**
     * 点击添加图片跳转
     */
    private OnAddPicClickListener onAddPicClickListener;

    public interface OnAddPicClickListener {
        void onAddPicClick();
    }


    public void setList(List<LocalMedia> list) {
        this.list = list;
    }

    @Override

    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = mInflater.inflate(R.layout.griditem, viewGroup, false);
        final ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public int getItemCount() {
        if (list.size() < selectMax) {
            return list.size() + 1;
        } else {
            return list.size();
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (isShowAddItem(position)) {
            return TYPE_CAMERA;
        } else {
            return TYPE_PICTURE;
        }
    }

    /**
     * 选择哪个布局
     *
     * @param position
     * @return
     */
    private boolean isShowAddItem(int position) {
//        int size = list.size() == 0 ? 0 : list.size();
        return position == list.size();
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        AppUtils.setImageMatch((Activity) context,holder.rlgiditem);
        //少于8张，显示继续添加的图标
        if (getItemViewType(position) == TYPE_CAMERA) {
            holder.mImg.setImageResource(R.mipmap.ic_addpic);
            holder.mImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onAddPicClickListener.onAddPicClick();
                }
            });
            holder.ll_del.setVisibility(View.INVISIBLE);
        } else {
            holder.ll_del.setVisibility(View.VISIBLE);
            holder.ll_del.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int index = holder.getAdapterPosition();
                    // 这里有时会返回-1造成数据下标越界,具体可参考getAdapterPosition()源码，
                    // 通过源码分析应该是bindViewHolder()暂未绘制完成导致，知道原因的也可联系我~感谢
                    if (index != RecyclerView.NO_POSITION) {
                        list.remove(index);
                        notifyItemRemoved(index);
//                        notifyItemRangeChanged(index, list.size());
                        DebugUtil.i("delete position:", index + "--->remove after:" + list.size());
                    }
                }
            });
            LocalMedia media = list.get(position);
            int mimeType = media.getMimeType(); //资源的 类型(图片 视频 音频)
            String path = "";
            if (media.isCut() && !media.isCompressed()) {
                // 裁剪过
                path = media.getCutPath();
            } else if (media.isCompressed() || (media.isCut() && media.isCompressed())) {
                // 压缩过,或者裁剪同时压缩过,以最终压缩过图片为准
                path = media.getCompressPath();
            } else {
                // 原图
                path = media.getPath();
            }
            // 图片
            if (media.isCompressed()) {
                Log.i("compress image result:", new File(media.getCompressPath()).length() / 1024 + "k");
                Log.i("压缩地址::", media.getCompressPath());
            }
            Log.i("原图地址::", media.getPath());
            RequestOptions options = new RequestOptions()
                    .centerCrop()
                    .placeholder(R.color.white)
                    .diskCacheStrategy(DiskCacheStrategy.ALL);
            Glide.with(holder.itemView.getContext())
                    .load(path)
                    .apply(options)
                    .into(holder.mImg);

        }
    }


    /**
     * adapter 的holder
     */
    public class ViewHolder extends RecyclerView.ViewHolder {

        private final RelativeLayout rlgiditem;
        ImageView mImg;
        LinearLayout ll_del;
//        TextView tv_duration;


        public ViewHolder(View view) {
            super(view);
            mImg = (ImageView) view.findViewById(R.id.imageView1);
            ll_del = (LinearLayout) view.findViewById(R.id.ll_del);
//            tv_duration = (TextView) view.findViewById(R.id.tv_duration);
            rlgiditem = ((RelativeLayout) view.findViewById(R.id.rl_giditem));
        }
    }
}
